package com.sumer.psychology.common.persistence.mongodb;

import com.mongodb.BasicDBObject;
import com.mongodb.DBObject;

/**
 * 包含简单增改的mongo
 * <p/>
 * Created by zhu on 14-10-12.
 */
public abstract class AbstractSimpleMongoRepository extends AbstractMongoRepository {
    /**
     * 增加一条
     *
     * @param data json
     */
    public void add(String data) {
        DBObject dbObject = initializeCreateData(data, key());
        db().getCollection(dbName()).insert(dbObject);
    }

    /**
     * 修改
     *
     * @param data json
     */
    public void change(String data) {
        DBObject changedObject = initializeData(data, key());
        DBObject queryObject = idCondition((String) changedObject.get("_id"));

        db().getCollection(dbName()).update(
                queryObject,
                new BasicDBObject("$set", changedObject));
    }

    protected abstract String key();
}
